import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../common/colors.dart';
import 'my_team_model.dart';

class JoinTeamPage extends StatefulWidget {
  const JoinTeamPage({Key? key}) : super(key: key);

  @override
  State<JoinTeamPage> createState() => _JoinTeamPageState();
}

class _JoinTeamPageState extends State<JoinTeamPage> {


  TextEditingController _editingController = TextEditingController();
  
  /// 加入团队
  requestDataWithJoinTeam(var team_id) async {

    var params = {
      'team_id':team_id
    };
    var json = await DioManager().kkRequest(Address.joinTeam,bodyParams: params);

    BotToast.showText(text: '${json['message']}');
  }


  MyTeamModel? model;
  ///所有团队
  requestDataWithAllTeam({String? search})async{
    var params = {
      'search':search
    };
    var json = await DioManager().kkRequest(Address.allTeams,
        params: params,method: 'get');

    MyTeamModel _model = MyTeamModel.fromJson(json);

    model = _model;

    setState(() {

    });

    // BotToast.showText(text: '${json['message']}');

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    requestDataWithAllTeam();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:
      BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('加入团队'),
      ),
      // AppBar(
      //   backgroundColor: AppColor.themeColor,
      //   leading: IconButton(
      //     onPressed: () {
      //       Get.back(result: 'refresh');
      //     },
      //     icon: Image.asset(
      //       'images/leftarr.png',
      //       width: 15,
      //       height: 15,
      //       color: Colors.white,
      //     ),
      //   ),
      //   title: Text('加入团队',style: TextStyle(color: Colors.white),),
      // ),
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: EdgeInsets.only(top: 15,left: 15),
              child: Text('加入团队',style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
            ),

            Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(18))),
                alignment: Alignment.centerLeft,
                margin: EdgeInsets.only(top: 15, left: 10, right: 10),
                padding: EdgeInsets.only(left: 5),
                height: 40,
                width: Get.width - 45,
                child: Row(
                  children: [
                    Icon(
                      Icons.search,
                      size: 20,
                      color: AppColor.smallTextColor,
                    ),
                    Container(
                      alignment: Alignment.center,
                      // color: Colors.red,
                      width: Get.width - 100,
                      height: 45,
                      child: TextField(
                        onChanged: (values) {
                          // controller.requestData(search: values);
                          requestDataWithAllTeam(search: values);
                        },
                        decoration: InputDecoration(
                            isCollapsed: true,
                            border: InputBorder.none,
                            hintText: '请输入关键词',
                            hintStyle: TextStyle(
                                color: AppColor.hintColor, fontSize: 13)),
                        style: TextStyle(
                            color: AppColor.smallTextColor, fontSize: 14),
                      ),
                    )
                  ],
                )),
            Expanded(child:
            ListView.builder(
              padding: EdgeInsets.only(top: 15),
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: (){
                    requestDataWithJoinTeam(model?.data?[index].id);
                  },
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [

                      Container(
                        width: Get.width,
                        color: Colors.white,
                        padding: EdgeInsets.all(15),
                        child: Text('${model?.data?[index].name}'),
                      ),
                      Container(
                        height: 0.5,
                        color: AppColor.lineColor,
                      )
                    ],
                  ),
                );
              },itemCount: model?.data?.length??0,)),
          ],
        ),
      ),
    );
  }
}
